﻿using Networking;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Chat
{
    public partial class frmClient : Form
    {
        private ClientManager Client;

        public frmClient()
        {
            InitializeComponent();
            TextBox.CheckForIllegalCrossThreadCalls = false;
            Button.CheckForIllegalCrossThreadCalls = false;
            CheckBox.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            btnConnect.Invoke((MethodInvoker)delegate
            {
                if (txtPort.Text.Length > 0)
                {
                    string ip = txtIP.Text;
                    int port = int.Parse(txtPort.Text);

                    if (ip.Length > 0 && port > 20)
                    {
                        Client = new ClientManager(ip, port);
                        Client.OnClientConnect += Client_OnClientConnect;
                        Client.OnServerShutdown += Client_OnServerShutdown;
                        Client.OnReceiveData += Client_OnReceiveData;
                        Client.Connect();
                        btnConnect.Enabled = false;
                    }
                    else
                    {
                        MessageBox.Show("Put an IP to connect.");
                    }
                }
                else
                {
                    MessageBox.Show("Put the Port to connect.");
                }
            });
        }

        private void Client_OnServerShutdown(int port)
        {
            Log("CLIENT: Server shutdown.");
            btnConnect.Enabled = true;
        }

        private void Client_OnReceiveData(Headers header, SubHeaders subheader, string data, int clientid = -1)
        {
            string text = "";
            if (clientid == -1) text = "Server"; else text = "Client " + clientid.ToString();
            Log("CLIENT: Received data from " + text + ".");
            Log("CLIENT: Data '" + data + "'.");
            Log("CLIENT: Header '" + header.ToString() + "'.");
            Log("CLIENT: Subheader '" + subheader.ToString() + "'.");
        }

        private void Client_OnClientConnect(int port)
        {
            Log("CLIENT: Connected on Port " + port.ToString() + ".");
            btnSend.Enabled = true;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            btnSend.Invoke((MethodInvoker)delegate
            {
                string message = txtMessage.Text;

                if (message.Length > 0)
                {
                    Client.SendData(SubHeaders.Text, message);
                }
                else
                {
                    Log("Put a message first.");
                }
            });
        }

        private void Log(string text)
        {
            txtChat.Invoke((MethodInvoker)delegate
            {
                txtChat.AppendText(text + "\r\n");
            });
        }
    }
}
